(function() {
    Nokia.OptionGroup = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				label: '',
				roundedCorners: true
            };

            instance.options = jQuery.extend(defaults, options);

            instance._super.apply(instance, [instance.options]);

			instance.registerData("optiongroup");

			instance.items = [];

			instance._create();

			instance._attachEvents();
        },

		_attachEvents: function() {
			var instance = this;
			var options = instance.options;
			var wrapperParent = instance.wrapper.parent();
			var Styles = instance.Styles;

			instance.wrapper.bind('mouseenter.optiongroup', function(event) {
				instance.wrapper.addClass(Styles.uiStateHover);

				if (options.roundedCorners) {
					Nokia.util.addBorderClass(wrapperParent, 'nokia-border-state-hover');
				}
			});

			instance.wrapper.bind('mouseleave.optiongroup', function(event) {
				instance.wrapper.removeClass(Styles.uiStateHover);

				if (options.roundedCorners) {
					Nokia.util.removeBorderClass(wrapperParent, 'nokia-border-state-hover');
				}
			});
		},

		addItem: function(item) {
			var instance = this;
			var element = instance.element;

			var optionWrapper = Nokia.dom.parseHTML('<div class="nokia-optiongroup-wrapper"></div>');

			if (!item.wrapper.data('optiongroup.originalParent')) {
				item.wrapper.data('optiongroup.originalParent', item.wrapper.parent());
			}

			Nokia.dom.append(optionWrapper, item.wrapper);

			Nokia.dom.append(instance.wrapper, optionWrapper);

			instance.items.push(item);
		},

		removeItem: function(item) {
			var instance = this;
			var element = instance.element;
			var index = jQuery.inArray(item, instance.items);

			if (index != -1) {
				item.wrapper.appendTo(
					item.wrapper.data('optiongroup.originalParent')
				);

				Nokia.util.arrayRemove(instance.items, index);
			}
		},

		getItems: function() {
			var instance = this;
			return instance.items;
		},


		getSelectedItems: function() {
			var instance = this;
			var element = instance.element;

			return instance.element.find(':checked');
		},

		_create: function() {
			var instance = this;
			var element = instance.element;
			var options = instance.options;
			var title = options.title;

			instance.wrapper = Nokia.dom.parseHTML('<div></div>');
			instance.titleWrapper = Nokia.dom.parseHTML('<div class="nokia-optiongroup-title"></div>');
			instance.titleWrapper.html(title);

			instance.wrapper.append(instance.titleWrapper);
			element.append(instance.wrapper);

			element.addClass('nokia-optiongroup');

			instance.wrapper.addClass('nokia-optiongroup-element ui-dialog-titlebar ui-state-default ui-corner-all ui-helper-clearfix');

			if (options.roundedCorners) {
				Nokia.util.applyRoundedCorners(instance.wrapper);
			};
		},

		Styles: {
			iconClass: '',
			labelClass: '',
			prefixBaseClass: '',
			pushedClass: '',
			wrapperClass: '',
			uiStateHover: 'ui-state-hover'
		}
    });
})();